package com.gress.shop.service.mail.impl;

import com.gress.shop.bo.Member;
import com.gress.shop.constants.EmailConstants;
import com.gress.shop.service.mail.IMailService;
import com.gress.shop.service.inner.IMemberInternalService;
import com.gress.shop.util.BaseUtil;
import com.gress.shop.util.RandomCodeUtil;
import com.gress.shop.vo.SwalFactory;
import com.gress.shop.vo.SwalReturn;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;


public abstract class AbstractMailService implements IMailService {

    @Resource
    private IMemberInternalService memberInternalService;

    @Resource
    private ThreadPoolTaskExecutor emailPool;

    /**
     * @param email   邮件
     * @param code    发送编码
     * @param session
     * @Date: 22:25 2018/2/17
     * @Description 注册邮件发送
     */
    public SwalReturn sendRegisterMailCode(String email, String code, HttpSession session) {
        Member member = memberInternalService.queryByEmail(email);
        if (null != member) {
            return SwalFactory.getWarnSwal(EmailConstants.EMAIL_EXIST_MESSAGE);
        }
        String attribute = (String) session.getAttribute(email);
        if (!StringUtils.isEmpty(attribute)) {
            SwalReturn back = SwalFactory.getWarnSwal();
            back.setText(EmailConstants.EMAIL_SEND);
            return back;
        }
        //生成6位验证码
        String sixCode = RandomCodeUtil.getValidationSixCode();
        // 存入session 失效时间1分钟
        session.setAttribute(BaseUtil.getEmailCode(email, code), sixCode);
        session.setMaxInactiveInterval(30);
        return sendEmailByTheal(email, "注册校验邮件", "欢迎注册，验证码为" + sixCode + "，有效期一分钟");
    }

    /**
     * @param to
     * @param subject
     * @param text
     * @Date: 17:26 2018/3/5
     * @Description: 用线程的方式去发送邮件
     **/
    public SwalReturn sendEmailByTheal(String to, String subject, String text) {
        emailPool.submit(new Runnable() {
            @Override
            public void run() {
                SwalReturn send = send(to, subject, text);
            }
        });
        return new SwalReturn();
    }


    /**
     * @param email
     * @param code
     * @param session
     * @Date: 19:09 2018/3/3
     * @Description 忘记密码发送邮件
     * @return :
     */
    public SwalReturn sendForgetMailCode(String email, String code, HttpSession session) {
        Member member = memberInternalService.queryByEmail(email);
        if (null == member) {
            return SwalFactory.getErrorSwal(EmailConstants.EMAIL_MESSAGE_EMPTY);
        }
        String attribute = (String) session.getAttribute(email);
        if (!StringUtils.isEmpty(attribute)) {
            SwalReturn back = SwalFactory.getWarnSwal();
            back.setText(EmailConstants.EMAIL_SEND);
            return back;
        }
        //生成6位验证码
        String sixCode = RandomCodeUtil.getValidationSixCode();
        // 存入session 失效时间1分钟
        session.setAttribute(BaseUtil.getEmailCode(email, code), sixCode);
        session.setMaxInactiveInterval(30);
        return sendEmailByTheal(email, "忘记密码", member.getName() + "找回用户密码，验证码为" + sixCode + "，有效期一分钟");
    }

    /**
     * @param code
     * @param emailCode
     * @param session
     * @Date: 20:02 2018/3/3
     * @Description: 邮件校验
     * @see  com.gress.shop.constants.EmailConstants
     */
    public String checkEqualCode(String code, String emailCode, HttpSession session) {
        String attribute = (String) session.getAttribute(emailCode);
        if (StringUtils.isEmpty(attribute)) {
            //过期
            return EmailConstants.EMAIL_OVERDUE;
        }

        if (attribute.equals(code)) {
            //邮件校验成功
            session.removeAttribute(emailCode);
            return EmailConstants.EMAIL_CHECK_SUCCESS;
        } else {
            //校验失败
            return EmailConstants.EMAIL_CHECK_FAILURE;
        }
    }
}
